<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace Qiniu;

class Etag
{
    private static function packArray($v, $a)
    {
        return call_user_func_array("pack", array_merge([$v], (array) $a));
    }
    private static function blockCount($fsize)
    {
        return intval(($fsize + Config::BLOCK_SIZE - 1) / Config::BLOCK_SIZE);
    }
    private static function calcSha1($data)
    {
        $sha1Str = sha1($data, true);
        $err = error_get_last();
        if($err !== NULL) {
            return [NULL, $err];
        }
        $byteArray = unpack("C*", $sha1Str);
        return [$byteArray, NULL];
    }
    public static function sum($filename)
    {
        $fhandler = fopen($filename, "r");
        $err = error_get_last();
        if($err !== NULL) {
            return [NULL, $err];
        }
        $fstat = fstat($fhandler);
        $fsize = $fstat["size"];
        if((int) $fsize === 0) {
            fclose($fhandler);
            return ["Fto5o-5ea0sNMlW_75VgGJCv2AcJ", NULL];
        }
        $blockCnt = self::blockCount($fsize);
        $sha1Buf = [];
        if($blockCnt <= 1) {
            array_push($sha1Buf, 22);
            $fdata = fread($fhandler, Config::BLOCK_SIZE);
            if($err !== NULL) {
                fclose($fhandler);
                return [NULL, $err];
            }
            list($sha1Code) = self::calcSha1($fdata);
            $sha1Buf = array_merge($sha1Buf, $sha1Code);
        } else {
            array_push($sha1Buf, 150);
            $sha1BlockBuf = [];
            for ($i = 0; $i < $blockCnt; $i++) {
                $fdata = fread($fhandler, Config::BLOCK_SIZE);
                list($sha1Code, $err) = self::calcSha1($fdata);
                if($err !== NULL) {
                    fclose($fhandler);
                    return [NULL, $err];
                }
                $sha1BlockBuf = array_merge($sha1BlockBuf, $sha1Code);
            }
            $tmpData = self::packArray("C*", $sha1BlockBuf);
            list($sha1Final) = self::calcSha1($tmpData);
            $sha1Buf = array_merge($sha1Buf, $sha1Final);
        }
        $etag = base64_urlSafeEncode(self::packArray("C*", $sha1Buf));
        return [$etag, NULL];
    }
}

?>